var express = require('express');
var router = express.Router();
var movie = require('../models/movie');

router.get('/', function (req, res, next) {
    res.render('index', {title: 'Express'})
});
//获得所有的电影列表
router.get('/list', function (req, res, next) {
    movie.findAll(function (err, allMovie) {
        res.json({status: 0, message: "获取成功", data: allMovie})
    })
});
//获得下载地址并将更新+1
//用户下载只返回下载地址
router.post('/download', function (req, res, next) {
    if (!req.body.movie_id) {
        res.json({status: 1, message: "电影id为空"})
    } else {
        movie.findById(req.body.movie_id, function (err, downloadMoive) {
            movie.updateOne({_id: req.body.movie_id}, {movieNumDownload: downloadMoive.movieNumDownload + 1}, function (err) {
                if (err) {
                    res.json({status: 1, message: "下载失败", data: err});
                } else {
                    res.json({status: 0, message: "下载成功", data: downloadMoive.movieDownload})
                }
            })
        })
    }
});
//获取相关电影的详细信息
router.post('/detail', function (req, res, next) {
    if (req.body.id) {
        movie.findById(req.body.id, function (err, getMovie) {
            res.json({status: 0, message: "获取成功", data: getMovie})
        })
    }else {
        res.json({ status: 1, message: "获取失败"})
    }
});
module.exports = router;

